<?php

namespace App\Domain\Saleman\Services;

use App\Domain\Saleman\Dao\SalemanDao;
use App\Domain\Saleman\Models\SalemanModel;
use App\Exceptions\BadRequestException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class SalemanService
{
    public function findAll(){
        return SalemanModel::where('state','1')->where('status','1')->get();
    }

    //创建修改
    public function setSaleman($param)
    {
        $saleDao = new SalemanDao();
        if ($param['id']) {
            $data = $saleDao->findById($param['id']);
            if (empty($data)) {
                throw new BadRequestException('业务员不存在');
            }
            $res = [
                'name' => $param['name'],
                'sex' => $param['sex'],
                'identity' => $param['identity'],
                'birthDay' => $param['birthDay'],
                'remark' => $param['remark'],
                'status' => $param['status'],
            ];
            $data = $saleDao->update($data, $res);
        } else {
            $data = $saleDao->create([
                'name' => $param['name'],
                'sex' => $param['sex'],
                'identity' => $param['identity'],
                'birthDay' => $param['birthDay'],
                'remark' => $param['remark'],
                'status' => $param['status'],
            ]);
        }
        return $data;
    }

    //删除
    public function del($id)
    {
        $saleDao = new SalemanDao();
        $data = $saleDao->del($id);
        return $data;
    }

    //修改状态
    public function changeState($param)
    {
        $saleDao = new SalemanDao();
        $sale = $saleDao->findById($param['id']);
        if ($sale['status'] == 1) {
            $res = ['status' => '0'];
            $sale = $saleDao->update($sale, $res);
        } else {
            $res = ['status' => '1'];
            $sale = $saleDao->update($sale, $res);
        }
        return $sale;
    }

    /**
     * 列表
     * @param $params
     * @return LengthAwarePaginator|Builder[]|Collection
     */
    public function list($params)
    {
        $query = SalemanModel::query()
            ->select('saleman.*')
            ->where('saleman.state', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['name']))
                $query->where('name', 'like', "%{$params['name']}%");
            if (isset($params['identity']))
                $query->where('identity', 'like', "%{$params['identity']}%");
            if (isset($params['birthDay']))
                $query->where('birthDay', $params['birthDay']);
            if (isset($params['remark']))
                $query->where('remark', 'like', "%{$params['remark']}%");
            if (isset($params['sex']))
                $query->where('sex', $params['sex']);
            if (isset($params['status']))
                $query->where('status', $params['status']);
        });
        if (isset($params['pageSize']) && isset($params['currentPage'])) {
            return $query->paginate($params['pageSize'], ['*'], 'page', $params['currentPage']);
        } else {
            return $query->get();
        }
    }
}
